import asyncio
import websockets


async def server(websocket, path):
    # 这里是处理websocket连接的代码
    name = await websocket.recv()
    print(f"Received message from {name}")
    greeting = f"Hello {name}!"
    await websocket.send(greeting)
    print(f"Sent greeting to {name}")


async def main():
    async with websockets.serve(server, "localhost", 8765):
        await asyncio.Future()  # 保持服务端运行

if __name__ == '__main__':
    asyncio.run(main())


